package homework.hoomework_10.phone;

import java.io.*;
import java.util.ArrayList;

/**
 * @author 朱
 * @date 2019042221:49
 */
public class PhoneDaoIO implements PhoneDao {

    @Override
    public void stock(Phone phone) {
        BufferedWriter writer = null;
        try {
            writer = new BufferedWriter(new FileWriter("phone.txt", true));
            String str = phone.getType() + "-" + phone.getName() + "-" + phone.getColor();
            writer.write(str);
            writer.newLine();
            writer.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (writer != null) {
                    writer.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void select_type(String type) {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader("phone.txt"));
            String str = null;
            while ((str = reader.readLine()) != null) {
                String[] s = str.split("-");
                if (s[0].equals(type)) {
                    System.out.println(s[0] + "-" + s[1] + "-" + s[2]);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    @Override
    public void select_name(String name) {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader("phone.txt"));
            String str = null;
            while ((str = reader.readLine()) != null) {
                String[] s = str.split("-");
                if (s[0].equals(name)) {
                    System.out.println(s[0] + "-" + s[1] + "-" + s[2]);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void selectAll() {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader("phone.txt"));
            String str = null;
            while ((str = reader.readLine()) != null) {
                String[] s = str.split("-");
                System.out.println(s[0] + "-" + s[1] + "-" + s[2]);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void delete(String type, String name) {
        ArrayList<Phone> arrayList = new ArrayList<Phone>();
        BufferedReader reader = null;
        BufferedWriter writer = null;
        try {
            reader = new BufferedReader(new FileReader("phone.txt"));
            String str = null;
            while ((str = reader.readLine()) != null) {
                String[] s = str.split("-");
                Phone phone = new Phone(s[0], s[1], s[2]);
                arrayList.add(phone);
            }
            boolean flag = false;
            for (int i = 0; i < arrayList.size(); i++) {
                if (arrayList.get(i).getType().equals(type) && arrayList.get(i).getName().equals(name)) {
                    flag = true;
                    arrayList.remove(arrayList.get(i));
                    writer = new BufferedWriter(new FileWriter("phone.txt"));
                    for (int j = 0; j < arrayList.size(); j++) {
                        Phone phone=arrayList.get(i);
                        String s1 = phone.getType() + "-" + phone.getName() + "-" + phone.getColor();
                        writer.write(s1);
                        writer.newLine();
                    }
                    writer.flush();
                    System.out.println("删除成功");
                    break;
                }
            }
            if (!flag){
                System.out.println("库存里没有这种手机");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if (reader!=null){
                    reader.close();
                }
                if (writer!=null){
                    writer.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
}
